package com.djp.smartapp.dialog;

import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.djp.smartapp.MainActivity;
import com.djp.smartapp.adapter.PeopleListAdapter;
import com.djp.smartapp.bean.PeopleResponse;
import com.djp.smartapp.bean.UserPeopleResponse;
import com.djp.smartapp.bean.WorkPostResponse;
import com.lenovo.manufacture.R;

import java.util.ArrayList;
import java.util.List;

public class GetPeopleDialog extends MyDialog {
    private static final String TAG = "GetPeopleDialog";
    private ListView peopleList;
    private List<UserPeopleResponse.UserPeople> allUserPeopleTemp;
    private List<PeopleResponse.People> allPeople;
    private List<WorkPostResponse.WorkPost> allWork;
    private List<UserPeopleResponse.UserPeople> allUserPeople;
    private GetPeopleDialog dialog;
    private PeopleListAdapter adapter;

    protected GetPeopleDialog(MainActivity context) {
        super(context);
        dialog = this;
    }

    @Override
    protected int setAlertLayout() {
        return R.layout.dialog_get_people;
    }

    @Override
    protected int setCancelButton() {
        return R.id.dialog_btn_people_cancel;
    }

    @Override
    protected int setFinishButton() {
        return R.id.dialog_btn_people_cancel;
    }

    @Override
    protected void setFinishButtonOnClick() {
        alertDialog.dismiss();
    }

    @Override
    protected void initView(View view) {
        peopleList = view.findViewById(R.id.dialog_li_get_people);

        view.findViewById(R.id.dialog_btn_people_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getList();
            }
        });
        getList();
    }

    private void getList() {
        new Thread() {
            @Override
            public void run() {
                requestToRefreshList();
            }
        }.start();
    }


    /**
     * 发送请求刷新list
     */
    public void requestToRefreshList() {
        allUserPeople = new ArrayList<>();
        //发送请求查询全部学生员工
        String allUserPeopleRes = okHttpUtil.getPostRes("UserPeople/getAll");
        UserPeopleResponse userPeopleResponse = gson.fromJson(allUserPeopleRes, UserPeopleResponse.class);
        if (userPeopleResponse.getStatus().equals("200")) {
            allUserPeopleTemp = userPeopleResponse.getData();

            //发送请求获取全部员工
            String allPeopleRes = okHttpUtil.getPostRes("People/getAll");
            PeopleResponse peopleResponse = gson.fromJson(allPeopleRes, PeopleResponse.class);

            //发送请求查询全部岗位
            String allWorkRes = okHttpUtil.getPostRes("WorkPost/getAll");
            WorkPostResponse workPostResponse = gson.fromJson(allWorkRes, WorkPostResponse.class);

            if (peopleResponse.getStatus().equals("200") && workPostResponse.getStatus().equals("200")) {
                allPeople = peopleResponse.getData();
                allWork = workPostResponse.getData();

                //双重for循环查找学生员工信息并添加到list
                UserPeopleResponse.UserPeople userPTemp;
                PeopleResponse.People pTemp;
                WorkPostResponse.WorkPost wTemp;

                for (int i = 0; i < allUserPeopleTemp.size(); i++) {
                    userPTemp = allUserPeopleTemp.get(i);
                    //Log.d(TAG, "run: 目前学生员工"+userPTemp.toString());
                    for (int j = 0; j < allPeople.size(); j++) {
                        pTemp = allPeople.get(j);

                        if (userPTemp.getPeopleId() == pTemp.getId()) {
                            //Log.d(TAG, "run: 已找到员工信息"+pTemp.toString());
                            userPTemp.setMoney(pTemp.getGold());
                            userPTemp.setName(pTemp.getPeopleName());
                            userPTemp.setWorkType(pTemp.getStatus());
                        }
                    }

                    for (int k = 0; k < allWork.size(); k++) {
                        wTemp = allWork.get(k);
                        if (userPTemp.getWorkPostId() == wTemp.getId()) {
                            userPTemp.setWorkPost(wTemp);
                        }
                    }

                    allUserPeople.add(userPTemp);
                }
                //Log.d(TAG, "run: "+allUserPeople);
            }

            //更新listview
            refreshListView();
        }
    }

    //更新listview的方法
    private void refreshListView() {
        context.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                //如果adapter已经初始化，则只更新adapter的数据
                if (adapter == null) {
                    Log.d(TAG, "run: 1");
                    adapter = new PeopleListAdapter(allUserPeople, context, dialog);
                } else {
                    adapter.setUserPeopleList(allUserPeople);
                }
                peopleList.setAdapter(adapter);
            }
        });
    }
}
